package com.mesystem.managedbean;

import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class GeneralMB {
	
	public static void mensajeInfo(String titutlo, String mensaje) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, titutlo, mensaje));
	}

	public static void mensajeError(String titutlo, String mensaje) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Informaci\363n", mensaje));
	}
	
	public static void putObjectInSession(String value, Object var) {
		ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
		HttpSession session = (HttpSession)context.getSession(true);
		session.setAttribute(value, var);
	}

	public static Object getObjectInSession(String value) {
		ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
		HttpSession sessionhttp = (HttpSession)context.getSession(true);
		return sessionhttp.getAttribute(value);
	}

	public static void removeObjectInSession(String value) {
		ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
		HttpSession session = (HttpSession)context.getSession(false);
		session.removeAttribute(value);
		session.invalidate();
	}
	
	public static void removeSession() {
		try {
			ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
			HttpSession session = (HttpSession)context.getSession(false);
			session.invalidate();
		} catch (Exception e) {
			
		}
	}
	
	public static void bloqueaComplete() {
		RequestContext context = RequestContext.getCurrentInstance();
		context.addPartialUpdateTarget("messages");
	}
	
	public static void ejecutaComplete(String comando) {
		RequestContext context = RequestContext.getCurrentInstance();
		context.execute(comando);
	}
	
	public static Map<String, String> getParameterMapRequest() {
		ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
		return context.getRequestParameterMap();
	}
	
	public Object getBean(String name) {
		ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
		return WebApplicationContextUtils.getRequiredWebApplicationContext((ServletContext)
				context.getContext()).getBean(name);
	}
}
